package com.db.pck.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.db.core.vo.PageResult;
import com.db.core.vo.R;
import com.db.pck.entity.Package;
import com.db.pck.mapper.PackageMapper;
import com.db.pck.service.PackageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.db.tenant.entity.Tenant;
import com.db.user.entity.User;
import com.db.user.utils.GetJwtToken;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 租户套餐表 服务实现类
 * </p>
 *
 * @author db
 * @since 2023-11-01
 */
@Service
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package> implements PackageService {

    @Transactional
    @Override
    public R toggleStatus(String pckId) {
        //查询当前状态
        Package aPackage = baseMapper.selectById(pckId);
        aPackage.setStatus(1-aPackage.getStatus());
        baseMapper.updateById(aPackage);
        return R.ok("success");
    }

    @Transactional
    @Override
    public R insert(Package apackage,String token) {
        User verify = GetJwtToken.verify(token);
        apackage.setId(IdWorker.getIdStr());
        apackage.setCreator(verify.getUserName());
        apackage.setCreateTime(new Date());
        apackage.setUpdater(verify.getUserName());
        apackage.setUpdateTime(new Date());
        apackage.setDeleted(false);
        baseMapper.insert(apackage);
        return R.ok("添加成功");
    }

    @Transactional
    @Override
    public R update(Package apackage,String token) {
        User verify = GetJwtToken.verify(token);
        apackage.setUpdater(verify.getUserName());
        apackage.setUpdateTime(new Date());
        baseMapper.updateById(apackage);
        return R.ok("修改成功");
    }

    @Transactional
    @Override
    public R deleteById(String pckId) {
        baseMapper.deleteById(pckId);
        return R.ok("删除成功");
    }


}
